package com.lgu.iab.cleandb.task;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import com.lgu.iab.cleandb.intf.ScheduleTask;

public class DeleteQuerySchedulerTask implements ScheduleTask {
	private Logger logger = LoggerFactory.getLogger(getClass());
	private String queryString;
	private String baseTable;
	private String baseColumn;
	private String limit;
	private String diffdate;
	
	private String taskId;
	private TriggerType triggerType;
	private String expression;
	
	private SimpleJdbcTemplate jdbcTemplate;
	
	public DeleteQuerySchedulerTask() {}
	
	public void init() {
		queryString = queryString.replace("{basetable}", baseTable);
		queryString = queryString.replace("{basecolumn}", baseColumn);
		queryString = queryString.replace("{limit}", limit);
		queryString = queryString.replace("{diffdate}", diffdate);
	}
	
	@Override
	public Object onTime() {
		logger.debug("@ Deleting Specification : \r\nBaseTable : "+baseTable+"\r\nBaseColumn : "+baseColumn+"\r\nLimit : "+limit+"\r\nDiffDate : "+diffdate);
		int total = 0;
		while(true) {
			int cnt = 0;
			logger.debug("## Deleting!");
			cnt = jdbcTemplate.update(queryString);
			logger.debug("@ Query string is \r\n"+queryString);
			if(cnt > 0) logger.debug("## Success! Deleted count : "+cnt);
			total += cnt;
			if(cnt < Integer.parseInt(limit)) break;
		}
		return total;  
	}
	
	public void setDataSource(DataSource dataSource) {
		jdbcTemplate = new SimpleJdbcTemplate(dataSource);
	}
	
	public String getQueryString() {
		return queryString;
	}

	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}

	public String getBaseTable() {
		return baseTable;
	}

	public void setBaseTable(String baseTable) {
		this.baseTable = baseTable;
	}

	public String getBaseColumn() {
		return baseColumn;
	}

	public void setBaseColumn(String baseColumn) {
		this.baseColumn = baseColumn;
	}

	public String getLimit() {
		return limit;
	}

	public void setLimit(String limit) {
		this.limit = limit;
	}

	public String getDiffdate() {
		return diffdate;
	}

	public void setDiffdate(String diffdate) {
		this.diffdate = diffdate;
	}

	public SimpleJdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(SimpleJdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	@Override
	public String getTaskId() {
		return this.taskId;
	}

	@Override
	public void setTriggerType(TriggerType triggerType) {
		this.triggerType = triggerType;
	}

	@Override
	public TriggerType getTriggerType() {
		return this.triggerType;
	}

	@Override
	public void setExpression(String expression) {
		this.expression = expression;
	}

	@Override
	public String getExpression() {
		return this.expression;
	}
}
